package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.entity.BaseAttrValue;
import com.atguigu.gmall.model.product.vo.BaseAttrInfoVO;
import com.atguigu.gmall.model.search.SearchAttr;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.model.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author jiamin
* @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
* @createDate 2023-08-14 20:41:58
*/
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
    implements BaseAttrInfoService{

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = baseMapper.attrInfoList(category1Id,category2Id,category3Id);
        return baseAttrInfoList;
    }

    @Transactional
    @Override
    public void saveAttrInfo(BaseAttrInfoVO baseAttrInfoVO) {
        //1. 先插入分类表
        BaseAttrInfo baseAttrInfo = new BaseAttrInfo();
        //将源对象属性值赋值到目标对象
        BeanUtils.copyProperties(baseAttrInfoVO, baseAttrInfo);
        //插入成功后，mybatis-plus会把id封装到对的id属性值里面
        baseMapper.insert(baseAttrInfo);

        //通过上述生成的id属性，插入到平台属性值表中
        List<BaseAttrValue> baseAttrValueList = baseAttrInfoVO.getAttrValueList().stream()
                .map(item -> {
                    BaseAttrValue baseAttrValue = new BaseAttrValue();
                    baseAttrValue.setValueName(item.getValueName());
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                    return baseAttrValue;
                }).collect(Collectors.toList());
        baseAttrValueService.saveBatch(baseAttrValueList);
    }

    @Override
    public void updateAttrInfo(BaseAttrInfoVO baseAttrInfoVO) {
        //TODO 先修改属性名信息
        this.lambdaUpdate()
                .eq(BaseAttrInfo::getId, baseAttrInfoVO.getId())
                .set(BaseAttrInfo::getAttrName, baseAttrInfoVO.getAttrName())
                .set(BaseAttrInfo::getCategoryId, baseAttrInfoVO.getCategoryId())
                .set(BaseAttrInfo::getCategoryLevel, baseAttrInfoVO.getCategoryLevel())
                .update();
        //todo 然后再修改属性值信息
        //1. 先删除用户删除的值数据，通过数据库与上传的id信息求差集
        //  not in 天然的差集
        List<Long> valueIds = baseAttrInfoVO.getAttrValueList().stream()
                .filter(item -> item.getId() != null)
                .map(BaseAttrInfoVO.AttrValueListDTO::getId)
                .collect(Collectors.toList());
        //2. 判断如果valueIds为空说明用户删除完了
        if (valueIds.size() != 0) {
            //删除完了直接全部删除
            baseAttrValueService.lambdaUpdate()
                    .eq(BaseAttrValue::getAttrId, baseAttrInfoVO.getId())
                    .notIn(BaseAttrValue::getId, valueIds)
                    .remove();
        } else {
            //没有删除完就求差集删除
            baseAttrValueService.lambdaUpdate()
                    .eq(BaseAttrValue::getAttrId, baseAttrInfoVO.getId())
                    .remove();
        }

        //3.新增用户先添加的
        List<BaseAttrValue> baseAttrValueList = baseAttrInfoVO.getAttrValueList().stream()
                .filter(item -> item.getId() == null)
                .map(item -> {
                    BaseAttrValue baseAttrValue = new BaseAttrValue();
                    baseAttrValue.setValueName(item.getValueName());
                    baseAttrValue.setAttrId(baseAttrInfoVO.getId());
                    return baseAttrValue;
                }).collect(Collectors.toList());
        //添加数据库
        baseAttrValueService.saveBatch(baseAttrValueList);

    }

    @Override
    public List<SearchAttr> getAttrInfoAndValue(Long skuId) {
        return baseMapper.getAttrInfoAndValue(skuId);
    }


}




